package com.gadgets.busi.bm06.service;

import com.gadgets.busi.bm06.dto.TradeInfoAddDTO;
import com.gadgets.busi.bm06.dto.TradeNodeQueryDTO;
import com.gadgets.busi.bm06.pojo.TradeNodeInfo;
import com.gadgets.busi.bm06.pojo.TradeNodeLink;
import com.gadgets.busi.bm06.repository.ITradeNodeInfoRepository;
import com.gadgets.busi.bm06.repository.ITradeNodeLinkRepository;
import com.gadgets.framework.common.dto.Pagination;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import com.gadgets.framework.common.util.PaginationUtil;
import com.gadgets.framework.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author UncleXiu
 * @version V1.0.0
 * @data 2022-02-27
 */
@Service
@Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
public class TradeInfoServiceImpl implements ITradeInfoSerivce {

    @Autowired
    private ITradeNodeInfoRepository tradeNodeInfoRepository;
    @Autowired
    private ITradeNodeLinkRepository tradeNodeLinkRepository;

    /**
     * 新增贸易节点
     *
     * @param tradeInfoAddDTO 新增信息
     * @throws GadgetsServiceException 异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
    @Override
    public void addTradeNodeInfo(TradeInfoAddDTO tradeInfoAddDTO) throws GadgetsServiceException {
        TradeNodeInfo nodeInfo = new TradeNodeInfo();
        nodeInfo.setNodeName(tradeInfoAddDTO.getNodeName());
        nodeInfo.setArea(tradeInfoAddDTO.getArea());
        nodeInfo.setType(tradeInfoAddDTO.getType());
        // 新增贸易节点信息，并获取ID
        long nodeId = tradeNodeInfoRepository.insertTradeNode(nodeInfo);
        Long[] nextNodeIds = StringUtils.splitStrToLong(tradeInfoAddDTO.getNextNodeIds());
        // 循环保存节点信息
        for (Long nextNodeId : nextNodeIds) {
            TradeNodeLink nodeLink = new TradeNodeLink();
            nodeLink.setNodeId(nodeInfo.getId());
            nodeLink.setLinkNodeId(nextNodeId);
            tradeNodeLinkRepository.insertNodeLink(nodeLink);
        }
    }

    /**
     * 贸易节点关联信息
     *
     * @param link 关联信息
     * @throws GadgetsServiceException 异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
    @Override
    public void addTradeLinkInfo(TradeNodeLink link) throws GadgetsServiceException {
        try {
            tradeNodeLinkRepository.insertNodeLink(link);
        } catch (Throwable e) {
            e.printStackTrace();
            throw new GadgetsServiceException("违反唯一性约束");
        }
    }

    /**
     * 分页条件查询贸易节点信息查询
     *
     * @param queryDTO   查询条件
     * @param pagination 分页条件
     * @return 查询结果
     * @throws GadgetsServiceException 异常信息
     */
    @Transactional(transactionManager = "gadgetsDbTransactionManage", rollbackFor = Throwable.class)
    @Override
    public List<TradeNodeInfo> selectTradeNodeInfo(TradeNodeQueryDTO queryDTO, Pagination pagination) throws GadgetsServiceException {
        PaginationUtil.bulidPaginationInfo(pagination);
        List<TradeNodeInfo> retList = tradeNodeInfoRepository.selectTradeNodeByCondition(queryDTO);
        PaginationUtil.getPaginationResultInfo(retList, pagination);
        return retList;
    }
}
